package com.back.baseModule.controller;

import com.back.baseModule.entity.SysUser;
import com.back.util.DateUtil;
import com.back.api.CommonResult;
import com.back.config.ConstConfig;
import com.back.baseModule.service.impl.SysUserServiceImpl;
import com.back.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/uploadFile")
public class UploadController {
    @Autowired
    private SysUserServiceImpl sysUserServiceImpl;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file, @RequestParam(value = "type", required = false) Long type,
                         @RequestParam(value = "user_id", required = false) String user_id,
                         @RequestParam(value = "edi", required = false) Long edi) {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        String fileName = file.getOriginalFilename();
        //获取文件后缀
        String extention = fileName.substring(fileName.lastIndexOf("."));
        String newName = UuidUtil.get32UUID() + extention;
        //获取日期文件夹
        String ffile = DateUtil.getDays();
        //文件分类
        String ClassFile = "photo";
        if (type != null && type == 3) {
            ClassFile = "excelFile";
        }
        if (type != null && type == 4) {
            ClassFile = "leaveFile";
        }

        String returnPath = "back/" + ClassFile + "/" + ffile + "/";

        //图片保存地址
        String filePath = ConstConfig.UPLOAD_PATH + returnPath;
        //正式地址
        //String filePath = ConstConfig.UPLOAD_PATH + ConstConfig.SYSTEM_CODE + "/testImg/" + ffile + "/";
        File dest = new File(filePath + newName);
        try {
            File fileParent = dest.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();
            }

            file.transferTo(dest);
            //如果是上传背景图片 修改用户背景图片
            if (edi != null && type != null && edi == 1 && type == 1) {
                Map<String, Object> map3 = new HashMap<String, Object>();
                SysUser sysUser = new SysUser();
                sysUser.setId(user_id);
                sysUser.setIcon(returnPath + newName);
                sysUserServiceImpl.updateById(sysUser);
            }

            return returnPath + newName;
        } catch (IOException e) {

        }
        return "";
    }


    @RequestMapping(value = "/deleteImgByPath", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult upload(@RequestParam("imgPath") String imgPath) {
        System.out.println("imgPath===" + imgPath);
        if (imgPath == null || imgPath.isEmpty()) {
            return CommonResult.failed("图片路径不能为空");
        }
        File file = new File(ConstConfig.UPLOAD_PATH + imgPath);
        System.out.println("imgurl===" + file.getPath());
        if (file.exists()) {
            file.delete();
            System.out.println("删除成功");
            return CommonResult.success("删除成功");
        }
        return null;
    }

    @RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
    @ResponseBody
    public String uploadFiles(@RequestParam MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        String fileName = file.getOriginalFilename();

        //获取文件后缀
        assert fileName != null;
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //String newName = UuidUtil.get32UUID() + suffix;
        String newName = fileName.substring(0, fileName.lastIndexOf("."))+"_"+ System.currentTimeMillis() + suffix;
        //获取日期文件夹
        String f_file = DateUtil.getDays();

        String returnPath = "back/file/" + f_file + "/";

        //图片保存地址
        String filePath = ConstConfig.UPLOAD_PATH + returnPath;

        File dest = new File(filePath + newName);
        try {
            File fileParent = dest.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();
            }

            file.transferTo(dest);

            return returnPath + newName;
        } catch (IOException e) {

        }

        return "";
    }

}
